<?php
class ModelController extends CController {
	public function filters() {
		return array (
			
			'accessControl' 
		);
	}
	public function accessRules() {
		return array (
			
			array (
				
				'allow', 
				'users' => array (
					
					'@' 
				) 
			), 
			array (
				
				'deny',  // deny all users
				'users' => array (
					
					'*' 
				), 
				'message' => '无权访问' 
			) 
		);
	}
	/***********模型管理************/
	public function actionShowAddModel() {
		$files = CommonFunc::read_dir ( Yii::app ()->params ['tempdir'] );
		foreach ( $files as $key => $v ) {
			$filename = basename ( $v, ".php" );
			if (strpos ( $filename, 'category' ) !== FALSE) {
				$category [] = $filename;
			} elseif (strpos ( $filename, 'list' ) !== FALSE) {
				$list [] = $filename;
			} elseif (strpos ( $filename, 'show' ) !== FALSE) {
				$show [] = $filename;
			}
		}
		$smarty = Yii::app ()->smarty;
		$models = new Models ( );
		$ret = $models->getModels ();
		$smarty->assign ( "category", $category );
		$smarty->assign ( "list", $list );
		$smarty->assign ( "show", $show );
		$smarty->assign ( "ret", $ret );
		$smarty->assign ( "body", "model/show_add_model" );
		$smarty->display ( "layouts.html" );
	}
	public function actionAddModel() {
		$models = new Models ( );
		$id = $models->setModels ( $_POST );
		if ($id) {
			$table = new Table ( );
			$table->CreateTable ( addslashes ( $_POST ['tablename'] ), $id );
			$msg = CommonFunc::AjaxCallbackMessage ( 'success' );
		} else {
			$msg = CommonFunc::AjaxCallbackMessage ( 'failure' );
		}
		echo $msg;
	}
	public function actionShowModelList() {
		$models = new Models ( );
		$ret = $models->getModels ();
		$smarty = Yii::app ()->smarty;
		$smarty->assign ( "ret", $ret );
		$smarty->assign ( "body", "model/list_model" );
		$smarty->display ( "layouts.html" );
	}
	public function actionDelModel($id) {
		$model = new Models ( );
		$model->deleteByPk ( $id );
		
		$modelfield = new ModelField ( );
		
		$this->actionShowModelList ();
	}
	//字段管理
	public function actionFieldManage(){
		$smarty = Yii::app ()->smarty;
		$smarty->assign ( "body", "model/fieldmanage" );
		$smarty->display ( "layouts.html" );
	}
	public function actionEditModel() {
	
	}
	public function  actionAddField(){
		$smarty = Yii::app ()->smarty;
		$smarty->assign ( "body", "model/field_add" );
		$smarty->display ( "layouts.html" );
	}
	/**********栏目管理************/
	public function actionShowAddColumn($id = null) {
		$model = new Models ( );
		$type = $model->getName ();
		$category = new Category ( );
		$ret = $category->getCategory ();
		$smarty = Yii::app ()->smarty;
		if ($id) {
			$smarty->assign ( "id", $id );
		}
		$smarty->assign ( "ret", $ret );
		$smarty->assign ( "type", $type );
		$smarty->assign ( "body", "model/show_add_column" );
		$smarty->display ( "layouts.html" );
	}
	public function actionShowColumnList() {
		$category = new Category ( );
		$model = new Models ( );
		$type = $model->getName ();
		$ret = $category->getCategory ();
		$smarty = Yii::app ()->smarty;
		$smarty->assign ( "type", $type );
		$smarty->assign ( "ret", $ret );
		$smarty->assign ( "body", "model/list_column" );
		$smarty->display ( "layouts.html" );
	}
	public function actionAddcolumn() {
		$category = new Category ( );
		if ($category->setColumn ( $_POST )) {
			$msg = CommonFunc::AjaxCallbackMessage ( 'success' );
		} else {
			$msg = CommonFunc::AjaxCallbackMessage ( 'failure' );
		}
		echo $msg;
	}
	public function actionShowEditColumn($id) {
		$model = new Models ( );
		$type = $model->getName ();
		$category = new Category ( );
		$ret = $category->getCategoryById ( $id );
		$column = $category->getCategory ();
		$smarty = Yii::app ()->smarty;
		$smarty->assign ( "id", $id );
		$smarty->assign ( "ret", $ret );
		$smarty->assign ( "type", $type );
		$smarty->assign ( "column", $column );
		$smarty->assign ( "body", "model/show_edit_column" );
		$smarty->display ( "layouts.html" );
	}
	public function actionEditcolumn() {
		$version = yii::app ()->session->get ( 'version' );
		$category = new Category ( );
		$a = $category->editCategory ( $_POST );
		print_r ( $a );
	
	}
	
	public function actionDelColumn($id) {
		$category = new Category ( );
		$category->deleteByPk ( $id );
		$this->actionShowColumnList ();
	}

}
?>